<template>
  <div>
    <UploadExcel :on-success="handleSuccess" :before-upload="beforeUpload" />
  </div>
</template>

<script>
import UploadExcel from '@/components/UploadExcel/index.vue'
import { importEmployee } from '@/api/employees'
import { transExcel } from '@/utils/transExcel'
export default {
  name: '',
  components: { UploadExcel },
  props: {},
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    handleSuccess({ header, results }) {
      console.log(header, results)
      const enData = transExcel(results)
      console.log(enData)
      this.doImport(enData)
    },
    async doImport(enData) {
      try {
        const res = await importEmployee(enData)
        console.log(res)
      } catch (error) {
        console.dir(error)
        this.$message.error(error.message)
      }
    },
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    }
  }
}
</script>

<style scoped lang='less'>

</style>
